﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Utils.ControlToLoad;
using Utils.Exceptions;
using DAL;

namespace Prototype1.EditControls
{
    public partial class ControlTemplateSimple : System.Web.UI.UserControl, IControlToLoadRW
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected override void OnPreRender(EventArgs e)
        {
            //Guid la = this.Id;
        }

        #region IControlToLoad Members

        public Guid Id
        {
            get
            {
                if (ViewState["Id1"] != null)
                    return (Guid)ViewState["Id1"];
                else
                    throw new ValueNotAvailableException();
            }
            set { ViewState["Id1"] = value; }
        }

        public void Save()
        {
            ExpenseLineSimple exp = new ExpenseLineSimple();
            exp.Id = this.Id;
            exp.Value = Convert.ToInt32(this.tb_Value.Text);
            exp.Save();
        }
        #endregion

        #region IControlToLoadRW Members

        public Guid IdR
        {
            get
            {
                if (ViewState["IdR1"] != null)
                    return (Guid)ViewState["IdR1"];
                else
                    throw new ValueNotAvailableException();
            }
            set { ViewState["IdR1"] = value; }
        }

        public void Bind(Guid Id, Guid IdR)
        {
            this.Id = Id;
            this.IdR = IdR;
        }

        #endregion

        private void LoadFields(ExpenseLineSimple e)
        {
            this.tb_Value.Text = Convert.ToString(e.Value);
        }

        #region IControlToLoadRW Members


        public void BindControls(Guid id)
        {
            ExpenseLineSimple e = ExpenseLineSimple.Get(this.Id);
            if (e != null)
                this.LoadFields(e);
        }

        #endregion
    }
}